package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;
    //http://localhost:8081/shoppingCart/clean
    @DeleteMapping("/shoppingCart/clean")
    public ResultInfo clean(){
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartService.remove(wrapper);
        return ResultInfo.success(null);
    }
    //http://localhost:8081/shoppingCart/sub
    @PostMapping("/shoppingCart/sub")
    public ResultInfo sub(@RequestBody Cart cart){
        Cart newcart=cartService.sub(cart);
        if (newcart!=null){
            return ResultInfo.success(newcart);
        }else {
            return ResultInfo.success(0);
        }
    }
    //http://localhost:8081/shoppingCart/add
    @PostMapping("/shoppingCart/add")
    public ResultInfo add(@RequestBody Cart cart){
        Cart newcart = cartService.add(cart);
        return ResultInfo.success(newcart);
    }
    //http://localhost:8081/shoppingCart/list
    @GetMapping("/shoppingCart/list")
        public ResultInfo findCart(){
        List<Cart> cartList=cartService.findCart();
        return ResultInfo.success(cartList);
    }
}
